package com.hxy.health.platform.controller;

import com.github.pagehelper.PageInfo;
import com.hxy.health.platform.pojo.Product;
import com.hxy.health.platform.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ProductController {
    @Autowired
    private ProductService productService;
    /**
     * 跳转到产品界面
     */
    @RequestMapping("page/product")
    public String productPage(ModelMap modelmap,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageInfo<Product> pageInfo = productService.getAll(pageSize,pageNum);
        modelmap.addAttribute("pageInfo", pageInfo);
        return "product";
    }

    /**
     * 跳转到产品详情界面
     */
    @RequestMapping("page/productDetail")
    public String productDetail(@RequestParam("id") Integer id, ModelMap modelMap) {
        Product product = productService.getById(id);
        modelMap.addAttribute("product", product);
        return "productDetail";
    }

    /**
     * 搜索产品
     */
    @RequestMapping("product/searchProduct")
    public String searchProduct(
                                @RequestParam("productName")String productName,
                                ModelMap modelMap,

                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageInfo<Product> pageInfo = productService.searchProduct(pageSize,pageNum,productName );

        modelMap.addAttribute("pageInfo", pageInfo);

        PageInfo<Product> pageInfos= productService.getAll(pageSize,pageNum);
        modelMap.addAttribute("pageInfos", pageInfos);

        return "product";
    }

}
